package utilities.httpserver;

import java.io.IOException;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class server 
	implements Runnable
{
	protected HttpServer server;
	
	public server(InetSocketAddress endpoint, int port)
	{
		try {
			server = HttpServer.create();
			server.bind(endpoint, 0);
			server.setExecutor(null);
		} catch (IOException e) { e.printStackTrace(); }	
	}
	
	public server(String hostname, int port)
	{
		this(new InetSocketAddress(hostname, port), port);
	}
	
	public server(int port)
	{
		this("localhost", port);
	}
	
	public void run()
	{
		server.start();
	}
	
	public void start()
	{
		new Thread(this).start();
	}
	
	public void stop()
	{
		server.stop(0);
	}
	
	public void addHandler(String context, HttpHandler handler)
	{
		server.createContext(context, handler);
	}
	
	public void removeHandler(String context)
	{
		server.removeContext(context);
	}
}
